<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keywords" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordsQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="logininfos" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="username" label="用户名" width="100" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话号码" width="120" sortable>
			</el-table-column>
      <el-table-column prop="email" label="邮箱地址" width="150" sortable>
      </el-table-column>
      <el-table-column prop="type" label="角色" width="100" sortable>
        <template scope="scope">
          <span style="color: #11b95c" v-if="scope.row.type==1">用户</span>
          <span  style="color: crimson" v-if="scope.row.type==0">管理员</span>
        </template>
      </el-table-column>
      <el-table-column prop="disable" label="状态" width="100" sortable>
        <template scope="scope">
          <span style="color: #11b95c" v-if="scope.row.disable==1">启用</span>
          <span  style="color: crimson" v-if="scope.row.disable==0">未启用</span>
        </template>
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="totals" style="float:right;">
			</el-pagination>
		</el-col>

    <!-- 添加或修改的对话框
    el-dialog - elementUi提供的对话框 = 模态框
      :title="title" - 动态改变标题
      :visible.sync="saveFormVisible"  - 模态框的显示状态
      :close-on-click-modal="false" - 关闭模态框只能通过模态框的x关闭，点击空白处不能关闭模态框
    el-form - 表单
      :model="saveForm" - 绑定表单对象
      label-width="80px" - 标签宽度
      :rules="saveFormRules" - 验证规则
      ref="saveForm" - 为当前模态框取名
      prop="name" - 如果当前字段要验证，就必须写，出现*
      auto-complete="off" - 不自动提示
-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" ref="saveForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱地址">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-radio-group v-model="saveForm.type">
            <el-radio class="radio" :label="1">用户</el-radio>
            <el-radio class="radio" :label="0">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.disable">
            <el-radio class="radio" :label="true">启用</el-radio>
            <el-radio class="radio" :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交			</el-button>
      </div>
    </el-dialog>

	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					keywords: ''
				},
        logininfos: [],
				totals: 0,
				currentPage: 1,
        pageSize: 3,
				listLoading: false,
				sels: [],//列表选中列

				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
        title: '',
				// 添加或界面数据
        saveForm: {
          id: null,
          username: '',
          phone: '',
          email: '',
          salt: '',
          password: '',
          type: null,
          disable: null
        }
			}
		},
		methods: {
      keywordsQuery(){
        this.currentPage = 1;
        this.getLogininfos();
      },
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getLogininfos();
			},
			//获取用户列表
			getLogininfos() {
				let para = {
					currentPage: this.currentPage,
          pageSize: this.pageSize,
					keywords: this.filters.keywords
				};
				this.listLoading = true;
        this.$http.post("/logininfo",para).then(result=>{
          this.totals = result.data.totals;
          this.logininfos = result.data.data;
          this.listLoading = false;
        }).catch(result=>{
          this.$message.error('请求错误');
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.delete("/logininfo/"+row.id).then(res =>{
            this.$message(res.data.msg);
            this.currentPage = 1;
            this.getLogininfos();
            this.listLoading = false;
          }).catch(res =>{
            this.$message({
              message: '系统错误，稍后尝试',
              type: 'error'
            });
          })

				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title='编辑';
        this.saveForm = Object.assign({}, row);
				this.saveFormVisible = true;
			},
			//显示新增界面
			handleAdd: function () {
        this.title='新增';
        this.saveForm = {
          id: null,
          username: '',
          phone: '',
          email: '',
          salt: '',
          password: '',
          age: null,
          type: 1,
          logininfo_id: null,
          shop_id: null
        };
				this.saveFormVisible = true;
			},
			// 提叫删除或修改
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              let para = Object.assign({}, this.saveForm);
              this.$http.put("/logininfo/", para).then(res => {
                this.$message(res.data.msg);
                this.saveLoading = false;
                this.saveFormVisible = false;
                this.getLogininfos();
              }).catch(res => {
                this.$message({
                  message: '系统错误，稍后尝试',
                  type: 'error'
                });
              });

            });
          }
        });
      },
      // 批量选择时参数
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.patch("/logininfo/", ids).then(res =>{
            this.$message(res.data.msg);
            this.currentPage = 1;
            this.listLoading = false;
            this.getLogininfos();
          }).catch(res =>{
            this.$message({
              message: '系统错误，稍后尝试',
              type: 'error'
            });
          })

				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getLogininfos();
		}
	}
</script>

<style scoped>

</style>